package com.flexrrb.controls
{
	import com.flexrrb.utils.Utils;
	
	import flash.events.TextEvent;
	
	import mx.containers.TitleWindow;
	import mx.controls.Text;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;

	public class SmartTitleWindow extends TitleWindow
	{
		private var _invalidateText:Boolean;
		private var _originalText:String;
		private var _text:String;
		private var _textField:Text;
		
		private var _detectEmail:Boolean;
		private var _detectPhoneNumber:Boolean;
		private var _detectURL:Boolean;
		
		private static var classConstructed:Boolean = constructClass();
		
		private static function constructClass():Boolean
		{
			var selector:CSSStyleDeclaration = StyleManager.getStyleDeclaration("SmartTitleWindow");
			if(!selector)
			{
				selector = new CSSStyleDeclaration();
			}
			
			selector.defaultFactory = function():void
			{
				this.textAlign = "center";
			}
			
			StyleManager.setStyleDeclaration("SmartTitleWindow", selector, false);
			
			return true;
		}
		
		public function SmartTitleWindow()
		{
			super();
		}
		
		public function get detectEmail():Boolean
		{
			return _detectEmail;
		}
		
		public function set detectPhoneNumber(value:Boolean):void
		{
			_detectPhoneNumber = value;
			invalidateProperties();
		}
		
		public function get detectPhoneNumber():Boolean
		{
			return _detectPhoneNumber;
		}
		
		public function set detectURL(value:Boolean):void
		{
			_detectURL = value;
			invalidateProperties();
		}
		
		public function get detectURL():Boolean
		{
			return _detectURL;
		}
		
		public function set detectEmail(value:Boolean):void
		{
			_detectEmail = value;
			invalidateProperties();
		}
		
		public function set text(value:String):void
		{
			_originalText = value;
			_invalidateText = true;
			invalidateProperties();
		}
		
		public function get text():String
		{
			return _text;
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			_textField = new Text();
			_textField.setStyle("textAlign", this.getStyle("textAlign"));
			_textField.setStyle("horizontalCenter", 0);
			_textField.setStyle("verticalCenter", 0);
			
			
			_textField.percentWidth = 100;
			//_textField.percentWidth = _textField.percentHeight = 100;
			_textField.addEventListener(TextEvent.LINK, onTextClick, false, 0, true);
			addChild(_textField);
		}  
		
		override protected function measure():void
		{
			super.measure();
			
			measuredWidth = 300;
			measuredHeight = 200;
		}
		
		override protected function commitProperties():void
		{
			_text = _originalText;
			if(detectEmail)
			{
				_text = Utils.makeURLClickable(text);
			}
			
			if(detectEmail)
			{
				_text = Utils.makeEmailClickable(text);
			}
			
			if(detectPhoneNumber)
			{
				_text = Utils.makePhoneNumberClickable(text);
			}
			
			_textField.htmlText = _text;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
		
		protected function onTextClick(event:TextEvent):void
		{
			
		}
	}
}